package com.example.springboot.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.springboot.common.Result;
import com.example.springboot.entity.Absent;
import com.example.springboot.entity.AdjustRoom;
import com.example.springboot.entity.Student;
import com.example.springboot.service.*;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController

@RequestMapping("/absent")
public class AbsentController {
    @Autowired
    private AbsentService absentService;


    /**
     * 根据用户名字查询缺寝记录信息
     */
    @GetMapping("/findbyname")
    public Result<?> findPage(
            @RequestParam String name,
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search) {
        Page<Absent> page = absentService.findbyname(name,pageNum, pageSize, search);
        if (page != null) {
            return Result.success(page);
        } else {
            return Result.error("-1", "查询失败");
        }
    }
    /**
     * 查询所有缺寝记录信息
     */
    @GetMapping("/find")
    public Result<?> findPage(
            @RequestParam(defaultValue = "1") Integer pageNum,
            @RequestParam(defaultValue = "10") Integer pageSize,
            @RequestParam(defaultValue = "") String search) {
        Page<Absent> page = absentService.find(pageNum, pageSize, search);
        if (page != null) {
            return Result.success(page);
        } else {
            return Result.error("-1", "查询失败");
        }
    }
    /**
     * 添加学生信息
     */
    @PostMapping("/add")
    public Result<?> add(@RequestBody Absent absent) {
        int i = absentService.add(absent);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "添加失败");
        }

    }
    /**
     * 删除学生缺寝记录信息
     */
    @DeleteMapping("/delete/{id}")
    public Result<?> delete(@PathVariable Integer id) {
        int i = absentService.deleteAbsent(id);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "删除失败");
        }
    }
    /**
     * 更新学生缺寝记录信息
     */
    @PutMapping("/update")
    public Result<?> update(@RequestBody Absent absent) {
        int i = absentService.updateAbsent(absent);
        if (i == 1) {
            return Result.success();
        } else {
            return Result.error("-1", "更新失败");
        }
    }
}
